package com.open.server.api.system;

import com.open.system.dispatch.IOnlineUserDispatch;
import com.open.system.model.OnlineUser;
import com.open.common.model.Page;
import com.open.common.model.PageRequest;
import com.open.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 在线用户
 * @author dashu03
 * @date 2025年07月24日 14:56
 * @description
 */
@RestController
@RequestMapping("/system/online")
public class OnlineUserApi {

    @Autowired
    private IOnlineUserDispatch onlineUserDispatch;

    /**
     * 分页查询
     *
     * @param pageRequest 分页查询条件
     * @return
     */
    @PostMapping("findPage")
    public Result<Page<OnlineUser>> findPage(@RequestBody PageRequest<OnlineUser> pageRequest) {
        long now = System.currentTimeMillis();
        Result<Page<OnlineUser>> result = new Result<>();
        result.setData(onlineUserDispatch.findPage(pageRequest.getPage(), pageRequest.getModel()));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 强退用户
     *
     * @param token 用户token
     * @return
     */
    @GetMapping("forceLogout")
    public Result<Boolean> forceLogout(@RequestParam("token") String token) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(onlineUserDispatch.forceLogout(token));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 在线用户数量
     * @return
     */
    @GetMapping("onlineCount")
    public Result<Long> onlineCount() {
        long now = System.currentTimeMillis();
        Result<Long> result = new Result<>();
        result.setData(onlineUserDispatch.onlineCount());
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }
}
